// File MessageDispatcher.h
//
// Purpose: A message dispatcher. Manages messages of the type 
//          Telegram. Instantiated as a singleton.
//
// Author : Bryson King (edited)
// Original Code Provided by: Mat Buckland 2002 (fup@ai-junkie.com)
// Date : 18 March 2013
// --------------------------------------------------------------
#ifndef MESSAGEDISPATCHER_H
#define MESSAGEDISPATCHER_H

#include <set>

#include "Telegram.h"

class CBaseGameEntity;

//to make code easier to read
const double SEND_MSG_IMMEDIATELY = 0.0f;
const int   NO_ADDITIONAL_INFO   = 0;

//to make life easier...
#define Dispatch CMessageDispatcher::Instance()

class CMessageDispatcher
{
private:  
  
	//a std::set is used as the container for the delayed messages
	//because of the benefit of automatic sorting and avoidance
	//of duplicates. Messages are sorted by their dispatch time.
	std::set<Telegram> PriorityQ;

	//this method is utilized by DispatchMessage or DispatchDelayedMessages.
	//This method calls the message handling member function of the receiving
	//entity, pReceiver, with the newly created telegram
	void Discharge(CBaseGameEntity* pReceiver, const Telegram& msg);

	CMessageDispatcher();

	//copy ctor and assignment should be private
	CMessageDispatcher(const CMessageDispatcher&);
	CMessageDispatcher& operator=(const CMessageDispatcher&);

public:
	//this class is a singleton
	static CMessageDispatcher* Instance();

	//send a message to another agent. Receiving agent is referenced by ID.
	void DispatchMessageBase(double delay, int sender, int receiver, int msg, void* ExtraInfo);

	//send out any delayed messages. This method is called each time through   
	//the main game loop.
	void DispatchDelayedMessages();
};

#endif